package com.dataart.training.crimequalification.dao;

import com.dataart.training.crimequalification.entity.FacultativeCrimeObjectEntity;
import com.dataart.training.crimequalification.entity.MainCrimeObjectEntity;
import com.dataart.training.crimequalification.entity.SupplementaryCrimeObjectEntity;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CrimeObjectDAOImpl implements CrimeObjectDAO {

    //-----------------------------------------------------------------

    @Autowired
    private SessionFactory sessionFactory;

    // ----------------------------------------------------------------

    /**
     *
     * @return
     */
    @Override
    public List<MainCrimeObjectEntity> getAllMainCrimeObjects() {
        return sessionFactory.getCurrentSession()
                .createQuery("from MainCrimeObjectEntity order by codeName").list();
    }

    /**
     *
     * @return
     */
    @Override
    public List<FacultativeCrimeObjectEntity> getAllFacultativeCrimeObjects() {
        return sessionFactory.getCurrentSession()
                .createQuery("from FacultativeCrimeObjectEntity order by codeName").list();
    }

    /**
     *
     * @return
     */
    @Override
    public List<SupplementaryCrimeObjectEntity> getAllSupplementaryCrimeObjects() {
        return sessionFactory.getCurrentSession()
                .createQuery("from SupplementaryCrimeObjectEntity order by codeName").list();
    }

    /**
     *
     * @param codename
     * @return
     */
    @Override
    public MainCrimeObjectEntity getMainCrimeObjectByCodename(String codename) {
        return (MainCrimeObjectEntity) sessionFactory.getCurrentSession().
                createCriteria(MainCrimeObjectEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     *
     * @param codename
     * @return
     */
    @Override
    public FacultativeCrimeObjectEntity getFacultativeCrimeObjectByCodename(String codename) {
        return (FacultativeCrimeObjectEntity) sessionFactory.getCurrentSession().
                createCriteria(FacultativeCrimeObjectEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }

    /**
     *
     * @param codename
     * @return
     */
    @Override
    public SupplementaryCrimeObjectEntity getSupplementaryCrimeObjectByCodename(String codename) {
        return (SupplementaryCrimeObjectEntity) sessionFactory.getCurrentSession().
                createCriteria(SupplementaryCrimeObjectEntity.class)
                .add(Restrictions.eq("codeName", codename)).uniqueResult();
    }
}

